
package com.eshop.we.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eshop.we.book.entity.Book;
import com.eshop.we.book.enums.DelEnum;
import com.eshop.we.book.enums.StatusEnum;
import com.eshop.we.business.BookBusiness;
import com.eshop.we.category.entity.Category;
import com.eshop.we.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangzhanguo
 * @create 2018-06-05 10:54
 **/
@RestController
@RequestMapping("/book")
public class BookController extends BaseController{
    @Autowired
    private BookBusiness bookBusiness;

    /**
     * 首页发布的图书
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public Result list(Page page){
        return Result.success(bookBusiness.selectPage(page));
    }

    /**
     * 添加图书
     * @param
     * @return
     */
    @RequestMapping("/add")
    public Result add(Book book){
        return Result.success(bookBusiness.insert(book));
    }

    /**
     * 编辑图书
     * @param
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Book book){
        return Result.success(bookBusiness.updateById(book));
    }

    /**
     * 删除图书
     * @param
     * @return
     */
    @RequestMapping("/del")
    public Result del(Book book){
        book.setIsDel(DelEnum.IS_DEL_YES.getCode());
        Boolean flag = bookBusiness.updateById(book);
        if(flag){
            return Result.success();
        }else{
            return Result.fail("删除失败");
        }
    }

    /**
     * 上架图书
     * @param
     * @return
     */
    @RequestMapping("/grounding")
    public Result grounding(Book book){
        book.setStatus(StatusEnum.STATUS_UP.getCode());
        Boolean flag = bookBusiness.updateById(book);
        if(flag){
            return Result.success();
        }else{
            return Result.fail("上架失败");
        }
    }

    /**
     * 下架图书
     * @param
     * @return
     */
    @RequestMapping("/undercarriage")
    public Result undercarriage(Book book){
        book.setStatus(StatusEnum.STATUS_DOWN.getCode());
        Boolean flag = bookBusiness.updateById(book);
        if(flag){
            return Result.success();
        }else{
            return Result.fail("下架失败");
        }
    }

    /**
     * 图书搜索
     * @param title
     * @param categoryId
     * @param type
     * @return
     */
    @GetMapping("/getcategorybooks")
    public Result getCategoryBooks(@RequestParam(required = false)String title,@RequestParam(required = false)Integer categoryId,@RequestParam(required = false)String type){
        EntityWrapper ew=new EntityWrapper<Category>();
        if(StringUtils.isNotBlank(title)){ ew.like("book_name",title); }
        if(categoryId!=null){ ew.eq("category_id",categoryId); }
        if(type!=null){ ew.like("type",type); }
        ew.orderBy("sort_num");

        return Result.success(bookBusiness.selectList(ew));
    }
}
